{extend name="common/layout" /}

{block name="body"}
<div class="container">
    <div class="layui-form">
        <!-- 邮件设置 -->
        <div class="layui-card">
            <div class="layui-card-header">邮件设置</div>
            <div class="layui-card-body">
                <!-- 发送的方式 -->
                <div class="layui-form-item">
                    <label for="smtp_type" class="layui-form-label">发送的方式:</label>
                    <div class="layui-input-block">
                        <div class="layui-col-md4">
                            <select name="smtp_type" id="smtp_type">
                                <option value="">请选择</option>
                                <option value="smtp" {if $detail.smtp_type=="smtp"}selected{/if}>SMTP</option>
                            </select>
                        </div>
                    </div>
                </div>
                <!-- SMTP服务 -->
                <div class="layui-form-item">
                    <label for="smtp_host" class="layui-form-label">SMTP服务:</label>
                    <div class="layui-input-block">
                        <div class="layui-col-md4">
                            <input type="text" id="smtp_host" name="smtp_host" value="{$detail.smtp_host}"
                                   class="layui-input" autocomplete="off" placeholder="smtp.163.com">
                        </div>
                    </div>
                </div>
                <!-- SMTP端口 -->
                <div class="layui-form-item">
                    <label for="smtp_port" class="layui-form-label">SMTP端口:</label>
                    <div class="layui-input-block">
                        <div class="layui-col-md4">
                            <input type="text" id="smtp_port" name="smtp_port" value="{$detail.smtp_port}"
                                   class="layui-input" autocomplete="off" placeholder="25">
                        </div>
                    </div>
                </div>
                <!-- SMTP账号 -->
                <div class="layui-form-item">
                    <label for="smtp_user" class="layui-form-label">SMTP账号:</label>
                    <div class="layui-input-block">
                        <div class="layui-col-md4">
                            <input type="text" id="smtp_user" name="smtp_user" value="{$detail.smtp_user}"
                                   class="layui-input" autocomplete="off">
                        </div>
                    </div>
                </div>
                <!-- SMTP密码 -->
                <div class="layui-form-item">
                    <label for="smtp_pass" class="layui-form-label">SMTP密码:</label>
                    <div class="layui-input-block">
                        <div class="layui-col-md4">
                            <input type="text" id="smtp_pass" name="smtp_pass" value="{$detail.smtp_pass}"
                                   class="layui-input" autocomplete="off">
                        </div>
                    </div>
                </div>
                <!-- SMTP验证 -->
                <div class="layui-form-item">
                    <label for="verify_type" class="layui-form-label">SMTP验证:</label>
                    <div class="layui-input-block">
                        <div class="layui-col-md4">
                            <select name="verify_type" id="verify_type">
                                <option value="">默认</option>
                                <option value="tls" {if $detail.verify_type=="tls"}selected{/if}>TLS</option>
                                <option value="ssl" {if $detail.verify_type=="ssl"}selected{/if}>SSL</option>
                            </select>
                        </div>
                    </div>
                </div>
                <!-- 发件人邮箱 -->
                <div class="layui-form-item">
                    <label for="from_user" class="layui-form-label">发件人邮箱:</label>
                    <div class="layui-input-block">
                        <div class="layui-col-md3">
                            <input type="text" id="from_user" name="from_user" value="{$detail.from_user}"
                                   class="layui-input" autocomplete="off">
                        </div>
                        <div class="layui-form-mid" style="padding:0 0 0 5px !important; margin-top:4px;">
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-default" lay-event="test">测试发送</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- 保存配置 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <button class="layui-btn layui-btn-default {:check_perms('save', false)}" lay-submit lay-filter="addForm">保存配置</button>
            </div>
        </div>
    </div>
</div>
{/block}

{block name="js"}
<script>
    layui.use('form', function () {
        let form = layui.form;

        form.on('submit(addForm)', function(data) {
            layer.confirm('确定保存当前配置吗?', function (index) {
                layer.close(index);
                waitUtil.ajax({
                    url: '{:route("setting.email/save")}',
                    type: "POST",
                    data: data.field
                });
            });
        });

        waitUtil.event({
            test: function () {
                let that = this;
                layer.prompt({title: '请输入测试接收邮箱', formType: 3, value: ''}, function (value, index) {
                    layer.close(index);
                    waitUtil.locking(that);
                    waitUtil.ajax({
                        url: '{:route("setting.email/test")}',
                        type: "POST",
                        data: {recipient: value}
                    }).then(() => {
                        waitUtil.unlock(that);
                    });
                });
            }
        })
    });
</script>
{/block}